package com.bnq.control.desktop.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.provider.Settings;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.bnq.control.R;
import com.oray.commonutils.utils.BuildConfig;

/**
 * @author: wanghongqiang
 * @data: 2023/8/7
 */
public class NotificationUtils {

    public static final String SECONDARY_CHANNEL = "second_channel_02";

    /**
     *
     * @param service
     * @param startId
     */
    public static void bindNotification(Service service, int startId) {
        try {
            Log.e("TAG", "bindNotification >>> ");
            Notification notification = new NotificationCompat.Builder(service, NotificationUtils.SECONDARY_CHANNEL)
                    .setDefaults(Notification.DEFAULT_LIGHTS)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("sunlogin")
                    .setContentText("connect desktop")
                    .build();
            if (BuildConfig.hasOreo()) {                  // Android O以上启动提升为前台服务
                service.startForeground(startId, notification);
            } else {
                NotificationManager manager = NotificationUtils.getNotificationManager(service);
                if (null != manager) manager.notify(startId, notification);
            }
        } catch (Exception e) {
            Log.e("TAG", "bindNotification >>> error " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 打开通知渠道id
     *
     * @param context
     */
    public static void openNotificationChannel(Context context) {
        if (BuildConfig.hasOreo() && null != context) {
            Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
            intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
            intent.putExtra(Settings.EXTRA_CHANNEL_ID, SECONDARY_CHANNEL);
            context.startActivity(intent);
        }
    }


    /**
     * 获取通知管理器
     */
    public static NotificationManager getNotificationManager(Context context) {
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        String channelName = "connect desktop";
        if (BuildConfig.hasOreo()) {
            NotificationChannel channel = new NotificationChannel(SECONDARY_CHANNEL, channelName, NotificationManager.IMPORTANCE_DEFAULT);
            channel.enableLights(true);
            channel.setLightColor(Color.BLUE);
            channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            if (manager != null) manager.createNotificationChannel(channel);
        }
        return manager;
    }

    /**
     * 通知渠道是否可用
     *
     * @param context
     * @return
     */
    public static boolean isChannelCanUse(Context context) {
        if (BuildConfig.hasOreo()) {
            NotificationManager notificationManager = getNotificationManager(context);
            NotificationChannel notificationChannel = notificationManager.getNotificationChannel(SECONDARY_CHANNEL);
            return notificationChannel.getImportance() != NotificationManager.IMPORTANCE_NONE;
        } else {
            return true;
        }
    }

    /**
     * 取消通知显示
     */
    public static void cancelNotification(Service service, int startId) {
        NotificationManager notificationManager = getNotificationManager(service);
        if (null != notificationManager) notificationManager.cancel(startId);
    }
}
